//# sourceURL=http://uMapApp/Earth/CameraPath
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "./ViewPort"
], function (
    mhjs,
    uMap,
    uEarth,
    ViewPort
) {

    function CameraPath(option) {

        var _this = this;
        _this.points = option && option.points;
        _this.app = option && option.app;

    }

    var _class = CameraPath;

    Object.defineProperties(_class.prototype, {
        points: {
            get: function () {
                return this._points;
            },
            set: function (array) {

                var _this = this;
                this._points = [];
                $.each(array, function (i, item) {
                    item.viewport = new ViewPort(item.viewport);
                    _this._points.push(item);
                });
            }
        },
        flyTo : {
            value: function (index) {

                var _this = this;
                if (_this.points[index]) {

                    _this.currentIndex = index;

                    if (index < _this.points.length - 1) {
                        _this.nextIndex = index + 1;
                    } else {
                        _this.nextIndex = undefined;
                    }

                    _this.currentPoint = _this.points[index];

                    _this.app.earth.camera.flyToBoundingSphere(
                        _this.currentPoint.viewport.boundingSphere,
                        {
                            offset  : _this.currentPoint.viewport.hpr,
                            duration: _this.currentPoint.duration || 5,
                            complete: function () {
                                if (_this.nextIndex !== undefined) {
                                    if (_this.currentPoint.delay) {
                                        setTimeout(function () {
                                            _this.flyTo(_this.nextIndex);
                                        }, _this.currentPoint.delay * 1000);
                                    } else {
                                        _this.flyTo(_this.nextIndex);
                                    }
                                } else {
                                    _this.stop();
                                }
                            }
                        }
                    );
                }
            }
        },
        start : {
            value: function () {
                var _this = this;
                _this.isStart = true;
                _this.flyTo(0);
                _this.trigger("start");
                _this.trigger("toggle");
            }
        },
        stop  : {
            value: function () {
                var _this = this;
                _this.app.earth.camera.cancelFlight();
                _this.currentIndex = undefined;
                _this.nextIndex = undefined;
                _this.currentPoint = undefined;
                _this.isStart = false;
                _this.trigger("stop");
                _this.trigger("toggle");
            }
        },
        toggle: {
            value: function (value) {
                if (this.isStart) {
                    this.stop();
                } else {
                    this.start();
                }
            }
        }
    });

    mhjs.EventInterface(_class);

    return _class;

});